<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      show-checkbox
      node-key="id"
      default-expand-all
      :props="{label:'name'}"
      check-strictly
    />
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hSubmit">确定</el-button>
      <el-button @click="$emit('close')">取消</el-button>
    </div>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { tranListToTreeData } from '@/utils'
import { assignPremission, getRolesDetail } from '@/api/setting'
export default {
  props: {
    curId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permIds: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async hSubmit() {
      this.permIds = this.$refs.tree.getCheckedKeys()
      // console.log(this.permIds)
      await assignPremission({ id: this.curId, permIds: this.permIds })
      this.$emit('close')
    },
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.list = tranListToTreeData(res.data)
        const detail = await getRolesDetail(this.curId)
        this.$refs.tree.setCheckedKeys(detail.data.permIds)

      // 保存数据
      } catch (err) {
        console.log('getPermissionList', err)
      }
    }
  }
}
</script>

<style>

</style>
